import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-07-15
 * Time: 23:08
 */

@WebServlet("/showRequest")
public class ShowRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder result = new StringBuilder();
        result.append(req.getProtocol());//HTTP/1.1
        result.append("<br>");
        result.append(req.getMethod());//GET
        result.append("<br>");
        result.append(req.getRequestURI());//  /hello_servlet/showRequest
        result.append("<br>");
        result.append(req.getQueryString());//null
        result.append("<br>");
        result.append(req.getContextPath());//   /hello_servlet
        result.append("<br>");

        result.append("=========================");
        result.append("<br>");

        Enumeration<String> headerNames = req.getHeaderNames();//枚举类型
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = req.getHeader(headerName);
            result.append(headerName).append(":").append(headerValue).append("<br>");
        }



        //设置body的类型
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(result.toString());
    }
}
